<?php
/**
 * datetime 2025/9/3 15:17
 */

namespace app\common\library;
use DateTime;
use think\Exception;
class DataUtils
{
    /**
     * 获取时间范围
     * @param string $type today|yesterday|last_month
     * @return array
     */
    public static function getTimeRange($type, $date = null)
    {
        $timestamp = $date ? strtotime($date) : time();

        switch ($type) {
            case 'today':
                $start = strtotime('today', $timestamp);
                $end = strtotime('tomorrow', $timestamp) - 1;
                break;
            case 'yesterday':
                $start = strtotime('yesterday', $timestamp);
                $end = strtotime('today', $timestamp) - 1;
                break;
            case 'last_month':
                $start = strtotime('first day of last month 00:00:00', $timestamp);
                $end = strtotime('first day of this month 00:00:00', $timestamp) - 1;
                break;
            case 'this_month':
                $start = strtotime('first day of this month 00:00:00', $timestamp);
                $end = strtotime('first day of next month 00:00:00', $timestamp) - 1;
                break;
            case 'this_week':
                $start = strtotime('monday this week 00:00:00', $timestamp);
                $end = strtotime('sunday this week 23:59:59', $timestamp);
                break;
            case 'last_week':
                $start = strtotime('monday last week 00:00:00', $timestamp);
                $end = strtotime('sunday last week 23:59:59', $timestamp);
                break;
            case 'this_year':
                $start = strtotime(date('Y-01-01 00:00:00', $timestamp));
                $end = strtotime(date('Y-12-31 23:59:59', $timestamp));
                break;
            case 'last_7_days':
                $start = strtotime('-6 days', strtotime('today'));
                $end = strtotime('tomorrow') - 1;
                break;
            case 'last_30_days':
                $start = strtotime('-29 days', strtotime('today'));
                $end = strtotime('tomorrow') - 1;
                break;
            default:
                $start = $end = $timestamp;
        }

        return [
            'start_time' => $start,
            'end_time' => $end,
            'start_date' => date('Y-m-d H:i:s', $start),
            'end_date' => date('Y-m-d H:i:s', $end),
            'start_date_short' => date('Y-m-d', $start),
            'end_date_short' => date('Y-m-d', $end)
        ];
    }

    //获取前6个月
    public static  function getLastSixMonths($num=9) {
        $months = [];
        $current = new DateTime();

        // 从当前月份开始，往前推5个月
        for ($i = $num; $i >= 0; $i--) {
            $date = new DateTime();
            $date->modify("first day of -$i months");

            $months[] = [
                'year' => $date->format('Y'),
                'month' => $date->format('m月'),
                'month_name' => $date->format('F'),
                'display' => $date->format('Y-m'),
                'date' => $date->format('Y-m'),
            ];
        }
        return $months;
    }

    //获取近10天日期
    public static function getLastTenDays($num=9)
    {
        $days = [];
        for ($i = $num; $i >= 0; $i--) {
            $timestamp = strtotime("-$i days");
            $date2 = date('Y-m-d', $timestamp);

            $startTimestamp = strtotime($date2);
            $endTimestamp = strtotime($date2 . ' 23:59:59');
            $days[] = [
                'date' => date('Y-m-d', $timestamp),
                'display' => date('m-d', $timestamp),
                'timestamp' => $timestamp,
                'day_of_week' => date('N', $timestamp), // 周几（1-7）
                'startTimestamp'=>$startTimestamp,
                'endTimestamp'=>$endTimestamp,
            ];
        }
        return $days;
    }

    /**
     * 输入Y-m 或者Y-m-d 获取时间戳范围
     * @param $dateString
     * @return array
     * @throws \Exception
     */
    public static function getTimestampRange($dateString)
    {
        try {
            if (strlen($dateString) === 10) { // Y-m-d 格式
                $start = new DateTime($dateString . ' 00:00:00');
                $end = new DateTime($dateString . ' 23:59:59');
            } elseif (strlen($dateString) === 7) { // Y-m 格式
                $start = new DateTime($dateString . '-01 00:00:00');
                $end = new DateTime($dateString . '-' . $start->format('t') . ' 23:59:59');
            } else {
                throw new Exception('不支持的日期格式');
            }

            return [
                'start_time' => $start->getTimestamp(),
                'end_time' => $end->getTimestamp(),
                'start_date' => $start->format('Y-m-d H:i:s'),
                'end_date' => $end->format('Y-m-d H:i:s')
            ];
        } catch (Exception $e) {
            throw new Exception('日期格式错误: ' . $e->getMessage());
        }
    }

    /**
     * @param int $days
     * @param false $includeTime
     * @return array
     * 获取最近60天的时间戳范围
     */
    public static function getRecentDaysRange($days = 60, $includeTime = false) {
        $now = time();

        if ($includeTime) {
            // 包含具体时间：从当前时间往前推
            $startTimestamp = strtotime("-$days days", $now);
            $endTimestamp = $now;
        } else {
            // 自然日范围：从今天00:00往前推
            $startTimestamp = strtotime("-$days days", strtotime('today 00:00:00'));
            $endTimestamp = strtotime('today 23:59:59');
        }

        return [
            'start_timestamp' => $startTimestamp,
            'end_timestamp' => $endTimestamp,
            'start_date' => date('Y-m-d H:i:s', $startTimestamp),
            'end_date' => date('Y-m-d H:i:s', $endTimestamp),
            'days' => $days
        ];
    }
}